package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.OrgPublishService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 联盟机构出版物管理
 */
@RestController
@RequestMapping("/bk")
public class OrgPublishController {

    private static final Logger logger = LogManager.getLogger(OrgPublishController.class);
    
    @Resource
    private OrgPublishService orgPublishService;

    @LoginRequired
    @GetMapping("/clickOrgPublish")
    @ApiOperation(value = "点击出版物事件")
    public JsonResult clickOrgPublish(
            @ApiParam(value = "出版物ID", required = true) @RequestParam("id") String id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);

            orgPublishService.clickOrgPublish(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgPublish")
    @ApiOperation(value = "新增出版物")
    public JsonResult addOrgPublish(
            @ApiParam(value = "出版物标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "出版物副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "概要") @RequestParam(value = "gerneral",required = false) String gerneral,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("gerneral", gerneral);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);

            orgPublishService.addOrgPublish(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgPublish")
    @ApiOperation(value = "更新出版物")
    public JsonResult updateOrgPublish(
            @ApiParam(value = "出版物ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "出版物标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "出版物副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "概要") @RequestParam(value = "gerneral",required = false) String gerneral,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("gerneral", gerneral);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("orgId", orgId);
            orgPublishService.updateOrgPublish(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgPublish")
    @ApiOperation(value = "删除出版物")
    public JsonResult deleteOrgPublish(
            @ApiParam(value = "出版物ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            orgPublishService.deleteOrgPublish(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrgPublish")
    @ApiOperation(value = "禁用或启用出版物")
    public JsonResult disableOrEnableOrgPublish(
            @ApiParam(value = "出版物ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgPublishService.disableOrEnableOrgPublish(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgPublish")
    @ApiOperation(value = "获取出版物列表")
    public JsonResult listOrgPublish(
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("typeId", typeId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgPublishService.listOrgPublish(pageData));
            result.setResultNum(orgPublishService.listOrgPublishCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addOrgPublishType")
    @ApiOperation(value = "新增出版物类型")
    public JsonResult addOrgPublishType(
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "出版物类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "出版物类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("names", names);
            pageData.put("remark", remark);

            orgPublishService.addOrgPublishType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgPublishType")
    @ApiOperation(value = "更新出版物类型")
    public JsonResult updateOrgPublishType(
            @ApiParam(value = "出版物类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "出版物类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "出版物类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("orgId", orgId);
            pageData.put("remark", remark);

            orgPublishService.updateOrgPublishType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgPublishType")
    @ApiOperation(value = "删除出版物类型")
    public JsonResult deleteOrgPublishType(
            @ApiParam(value = "出版物类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            orgPublishService.deleteOrgPublishType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableOrgPublishType")
    @ApiOperation(value = "禁用或启用出版物")
    public JsonResult disableOrEnableOrgPublishType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            orgPublishService.disableOrEnableOrgPublishType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgPublishType")
    @ApiOperation(value = "获取出版物类型列表")
    public JsonResult listOrgPublishType(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(orgPublishService.listOrgPublishType(pageData));
            result.setResultNum(orgPublishService.listOrgPublishTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listOrgPublishTypeAll")
    @ApiOperation(value = "获取出版物类型列表所有")
    public JsonResult listOrgPublishTypeAll(
            @ApiParam(value = "机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);

            result.setResult(orgPublishService.listOrgPublishTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }




}
